// pages/invite/store.js
import { Store } from '../../model/store.js';
var store = new Store();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      hidden: false,
      store_id: '',
      name: '',
      phone: '',
      desc: '',
      loading: false,
      StoreGroupList:[],
      salName:'',
      id:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const scene = decodeURIComponent(options.scene)
    if (scene && scene != 'undefined'){
        wx.showLoading({
            title: '加载中',
        });
        this.getStoreUser(scene);
    } else {
        wx.showModal({
            title: '温馨提示',
            content: '二维码信息为空!',
            showCancel: false,
            confirmText: '返回首页',
            confirmColor: '#F3474E',
            success:function(res){
                if (res.confirm){
                    wx.switchTab({
                        url: '/pages/home/home',
                    })
                }
            }
        });
    }
  },
    /**
     * 创建团员
     */
    createStoreUser() {
        if(this.data.loading) {
            return false
        }
        this.setData({
            loading: true
        })
        var data = {
            store_id: this.data.store_id,
            name: this.data.name,
            phone: this.data.phone,
            desc: this.data.desc,
            group_id:this.data.id
        }
       store.createStoreUser(data,(isVild,result)=>{
            if(isVild) {
                wx.showModal({
                    title: '温馨提示',
                    content: '绑定成功!',
                    showCancel: false,
                    confirmText: '返回首页',
                    confirmColor: '#F3474E',
                    success:function(res){
                        if (res.confirm){
                            wx.switchTab({
                                url: '/pages/home/home',
                            })
                        }
                    }
                });
            }else {
                this.setData({
                    loading: false
                })
                for (var tmp in result.msg) {
                    wx.showToast({
                        title: result.msg[tmp],
                        icon: 'none',
                        duration: 2000
                    })
                }
            }
       })
    },

  /**
   * 查看当前用户的店员状态
   */
  getStoreUser(scene){
      var that = this;
      store.checkStoreUser({scene: scene},(isVild,result)=>{
          if(isVild) {
                this.setData({
                    store_id: result.store_id,
                    hidden: true,
                });
                that.getStoreGroup(that.data.store_id);
                wx.hideLoading();
          } else {

             wx.hideLoading();
              for (var tmp in result.msg) {
                  wx.showModal({
                      title: '温馨提示',
                      content: result.msg[tmp],
                      showCancel: false,
                      confirmText: '返回首页',
                      confirmColor: '#F3474E',
                      success:function(res){
                          if (res.confirm){
                              wx.switchTab({
                                  url: '/pages/home/home',
                              })
                          }
                      }
                  });
                  return false
              }
          }
      })
  },

  /**
   * 查看当前用户的店员状态
   */
  getStoreGroup(id){
      var that = this;
    store.getStoreGroup(id,(result)=>{
       that.setData({
        StoreGroupList:result
       })
    })
  },
  // 。。。选择业务员
  bindPickerChange(e){
    let value = this.data.StoreGroupList[e.detail.value]
    this.setData({
      salName: value.name,
      id:value.id
    })
  },

})
